﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;

namespace DLV.BackEnd.Pages
{
    public partial class EditUnility : AdminPageBase
    {
        private UtilityInfo _utilityInfo = new UtilityInfo();
        private readonly UtilityController _utilityController = new UtilityController();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["UtilityId"]))
                _utilityInfo = _utilityController.GetUtility(int.Parse(Request.QueryString["UtilityId"]));

            if (Page.IsPostBack) return;

            LoadStatus();
            LoadUser();
            LoadUtilityType();

            if (_utilityInfo.UtilityId == -1) return;

            RenderForm();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListUtility.aspx?xml=Utility");
        }

        private void SaveData()
        {
            _utilityInfo.UtilityName = txtUtilityName.Text;
            _utilityInfo.Status = Convert.ToInt32(ddlStatus.SelectedValue);
            _utilityInfo.UtilityType = int.Parse(ddlUtilityType.SelectedValue);

            if (_utilityInfo.UtilityId == -1)
            {
                _utilityInfo.CreatedBy = ddlCreatedBy.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlCreatedBy.SelectedValue);
                _utilityInfo.CreatedDate = DateTime.Now;
                _utilityController.InsertUtility(_utilityInfo);
            }
            else
            {
                _utilityInfo.UpdatedBy = UserId;
                _utilityInfo.UpdatedDate = DateTime.Now;
                _utilityController.UpdateUtility(_utilityInfo);
            }

            Response.Redirect("/Pages/ListUtility.aspx?xml=Utility");
        }

        private void RenderForm()
        {
            txtUtilityName.Text = _utilityInfo.UtilityName;
            ddlStatus.SelectedValue = _utilityInfo.Status.ToString();
            ddlCreatedBy.SelectedValue = _utilityInfo.CreatedBy.ToString();
            ddlUtilityType.SelectedValue = _utilityInfo.UtilityType.ToString();
        }

        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
        private void LoadUtilityType()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/UtilityType.xml"));
            var dv = ds.Tables["UtilityType"].DefaultView;
            ddlUtilityType.DataTextField = "Name";
            ddlUtilityType.DataValueField = "ID";
            ddlUtilityType.DataSource = dv;
            ddlUtilityType.DataBind();

            ddlUtilityType.Items.Insert(0, new ListItem("--- Chọn loại tiện ích ---", "-1"));
        }
        private void LoadUser()
        {
            ddlCreatedBy.DataSource = new AdminUserController().ListAdminUser();
            ddlCreatedBy.DataTextField = "UserName";
            ddlCreatedBy.DataValueField = "UserId";
            ddlCreatedBy.DataBind();

            ddlCreatedBy.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }
    }
}